<template>
  <div class="app-container">

    <!-- <div class="grid-content bg-purple-dark">
        <el-form :inline="true" :model="formInline" ref="searchForm" class="demo-form-inline"></el-form>
          <el-button type="primary" @click="handleAdd()">新增</el-button>
          <el-form-item label="职员姓名">
          <el-input v-model="formInline.name" placeholder="职员姓名"></el-input>
          </el-form-item>
          <el-button type="primary" @click="selectAll">查询</el-button>
          <el-button type="primary" plain size="small" @click="resetSearchForm('searchForm')" icon="el-icon-refresh">
            重置
          </el-button> -->
    <el-form :inline="true" :model="formInline" ref="searchForm" class="demo-form-inline">
      <el-form-item>
        <el-button type="danger" size="small" @click="deleteBatch" icon="el-icon-delete">批量删除</el-button>
        <el-button type="warning" size="small" @click="handleAdd" icon="el-icon-plus">新增</el-button>
      </el-form-item>
      <el-form-item label="职员姓名">
        <el-input v-model="formInline.name" placeholder="职员姓名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="selectAll">查询</el-button>
        <el-button type="primary" plain size="small" @click="resetSearchForm('searchForm')" icon="el-icon-refresh">
          重置
        </el-button>
        <!-- <el-button type="primary" plain size="small" @click="dialogExcelVisible = true" icon="el-icon-upload2">导入
          </el-button>
          <el-button type="primary" plain size="small" @click="downloadExcel" icon="el-icon-download">导出</el-button> -->
      </el-form-item>
    </el-form>

    <el-table :data="empList" @selection-change="handleSelectionChange" style="width: 100%;  margin-bottom: 20px"
      row-key="id" border default-expand-all :tree-props="{ children: 'children' }">
      <!-- <el-table-column prop="id" label="id" sortable width="180">
              </el-table-column> -->
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="name" label="职员姓名" sortable width="180">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" sortable width="180">
      </el-table-column>
      <el-table-column prop="tdepartment.name" label="所属部门" sortable width="180">
      </el-table-column>

      <!-- <el-table-column prop="id" label="id" sortable width="180">
              </el-table-column> -->
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate('编辑职员', scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- </div> -->

    <!-- </el-row> -->
    <!-- dialog    -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="职员姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" :label-width="formLabelWidth">
          <el-select v-model="form.depId" placeholder="请选择">
            <el-option v-for="item in departments" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEmp">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { emp, addEmp, deleteByIds, deleteById, updateEmp } from '@/api/emp'
import { departments } from '@/api/depart'
export default {
  name: 'emp',
  data() {
    return {
      empList: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      dialogFormVisible: false,
      form: {
        id: '',
        depId: '',
        name: '',
        phone: ''
  
      },
      formInline: {
        name: ''
      },
      departments: [],
      ids: [],
      title: '添加职员'
    }
  },
  methods: {
    list() {
      emp({ pageNum: this.currentPage, pageSize: this.pageSize, name: this.formInline.name }).then(res => {
        this.empList = res.data.list
        this.total = res.data.total
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.list()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.list()
    },
    selectAll() {
      this.currentPage = 1
      this.list()
    },
    resetSearchForm(formName) {
      this.$refs[formName].resetFields()
      this.currentPage = 1
      this.formInline.name = ''
      this.list()
    },
    handleAdd() {
      this.title = '添加职员'
      departments().then(res => {
        this.departments = res.data
        this.dialogFormVisible = true
        this.form = {}
      })

    },
    // handleAdd() {
    //   this.title = '添加职员'
    //   this.dialogFormVisible = true
    //   this.from = {}
    // },
    addEmp(formName) {
      console.log("line159----------", this.title)
      if (this.title == "添加职员") {
        console.log("line161----------", this.form)
        addEmp(this.form).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'info',
              message: '添加成功!'
            })
            this.dialogFormVisible = false
            this.list()
          }
        })
      }
      if (this.title == "编辑职员") {
        updateEmp(this.form).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'info',
              message: '编辑成功!'
            })
            this.dialogFormVisible = false
            this.list()
          }
        })
      }

    },
    handleUpdate(title, index, row) {
      console.log("line186----------", row)
      this.title = title
      departments().then(res => {
        this.departments = res.data
        this.dialogFormVisible = true
      })
      this.dialogFormVisible = true
      this.form = row
    },
    handleDelete(row) {
      console.log("line157----------", row)
      this.$confirm('此操作将永久删除id为' + row.id + '的客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(row.id).then(() => {
          if (row.code == 200) {
            this.$message({
              type: 'info',
              message: '删除成功!'
            })
          }
        })
        this.list()
      }).catch(() => {
        this.list()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange(val) {
      this.ids = [];
      for (let i = 0; i < val.length; i++) {
        this.ids.push(val[i].id);
      }
    },
    deleteBatch() {
      this.$confirm('此操作将永久删除选中的' + this.ids.length + '个客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteByIds(this.ids).then(() => {
          this.$message({
            type: 'info',
            message: '删除成功!'
          })
          this.list()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
  },
  created() {
    this.list()
  },
}

</script>
<style scoped></style>
